<template>
    <table-view :show-block-control="false">
        <template slot="formSlot">
            <el-form ref="form" :model="form" label-width="80px" :inline="true">
                <el-form-item label="编码" prop="name">
                    <el-input v-model="form.code" size="small"></el-input>
                </el-form-item>
                <el-form-item label="名称" prop="name">
                    <el-input v-model="form.name" size="small"></el-input>
                </el-form-item>
            </el-form>
        </template>

        <div slot="btnBeforeSlot">
            <el-button type="info" plain icon="el-icon-document-add" size="small" @click="handleCustomAdd">新增</el-button>
        </div>
        <div slot = "btnAfterSlot">
            <el-button type="info" plain icon="el-icon-search" size="small" @click="handleSearch">查询</el-button>
            <el-button type="info" plain icon="el-icon-s-release" size="small" @click="handleRest">重置</el-button>
        </div>

        <template slot="tableSlot">
            <el-table v-loading="loading" :data="tableData" stripe height="100%" size="small" row-key="id"
                      width="100%" :tree-props="{children: 'children'}">
                <el-table-column prop="code" label="字典编码"></el-table-column>
                <el-table-column prop="name" label="字典名"></el-table-column>
                <el-table-column prop="enabled" label="状态" width="200">
                    <template slot-scope="scope">
                        <el-tag v-for="(item,index) in enableds"
                                v-if="scope.row.enabled == item.value && scope.row.enabled == enableds[0].value" :key="index">{{item.label}}</el-tag>
                        <el-tag v-for="(item,index) in enableds"
                                v-if="scope.row.enabled == item.value && scope.row.enabled == enableds[1].value" :key="index" type="danger">{{item.label}}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="type" label="字典类型"  width="200">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.type == 1 && scope.row.parentId == 0" >系统类型</el-tag>
                        <el-tag v-else-if="scope.row.type == 0  && scope.row.parentId == 0" >业务类型</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="sort" label="排序" width="150"></el-table-column>
                <el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
                <el-table-column prop="updateTime" label="更新时间" width="180"></el-table-column>
                <el-table-column label="操作" fixed="right" width="200" align="center">
                    <template slot-scope="scope">
                        <el-tooltip class="item" effect="dark" content="编辑" placement="top-start">
                            <el-button type="primary" plain size="mini"icon="el-icon-edit"
                                       @click="handleCustomAdd(scope.row)" class="tableBtn"></el-button>
                        </el-tooltip>
                        <el-tooltip class="item" effect="dark" content="删除" placement="top-start">
                            <el-button type="danger" plain size="mini" icon="el-icon-delete"
                                       @click="handleCustomDel(scope.row)" class="tableBtn"></el-button>
                        </el-tooltip>
                        <el-tooltip class="item" effect="dark" content="查看" placement="top-start">
                            <el-button type="info" plain size="mini"  icon="el-icon-search"
                                       @click="handleDetail(scope.row)" class="tableBtn"></el-button>
                        </el-tooltip>
                        <template v-if="scope.row.parentId == 0">
                            <el-tooltip class="item" effect="dark" content="添加子项" placement="top-start">
                                <el-button type="danger" plain size="mini" icon="el-icon-platform-eleme"
                                           @click="handleCustomChildrenAdd(scope.row)" class="tableBtn"></el-button>
                            </el-tooltip>
                        </template>
                    </template>
                </el-table-column>
            </el-table>
        </template>

        <template slot="paginationSlot">
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="page.pageNum"
                :page-size="page.pageSize"
                :total="page.total"
                :page-sizes="[10, 20, 30, 40]"
                layout="total, sizes, prev, pager, next, jumper">
            </el-pagination>
        </template>

        <template slot="otherSlot">
            <dialog-view ref="dialogRef" :title="dialogParams.title" :width="dialogParams.width">
                <template slot="dialogSlot">
                    <component :is="dialogParams.componentName" :editData="editRow"
                               @submitSuccess="handleSearch"></component>
                </template>
            </dialog-view>
        </template>
    </table-view>
</template>

<script>
    import TableView from '@/components/tableView'
    import DialogView from '@/components/dialogView'
    import tableMixin from '@/mixins/table.mixin'
    import {mapActions, mapMutations, mapState} from 'vuex'
    export default {
        mixins: [tableMixin],
        data() {
            return{
                searchApi: this.$api.system.dic.list,
                deleteApi: this.$api.system.dic.delete,
                title: '数据字典',
                form: {
                    name: ''
                }
            }
        },
        components: {
            TableView,
            DialogView,
            Add: () => import('./add'),
            Detail: () => import('./detail')
        },
        computed: {
            ...mapState('dic', ['enableds'])
        },
        methods: {
            ...mapActions('dic', ['doEnabledOfBasic']),
            handleCustomAdd(row) {
                if(row.constructor.name == 'MouseEvent') {
                    this.handleAdd({type: 1, enabled: 1,isDefault: 0, sort: 0});
                } else {
                    this.handleAdd(row);
                }
            },
            handleCustomChildrenAdd(row) {
                this.handleAdd({type: 1, enabled: 1, isDefault: 0 ,sort: 0, parentId: row.id});
            },
            handleCustomDel(row) {
                this.handleDel(row);
            }
        },
        mounted() {
            // 数据字典启停状态
            this.doEnabledOfBasic(this.$appConstant.DIC_CODE.SYSTEM_ENABLED);
        }
    }
</script>

<style scoped lang="less">
    @import "../../../assets/css/common.less";
</style>

